﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Collections;
using System.Net;
using System.Diagnostics;

namespace PenLiveUpdate {
    public partial class FormLiveUpdate : Form {

        List<DownloadFileInfo> downloadFileList = null;
        AutoUpdater au;

        public FormLiveUpdate () {
            InitializeComponent( );
        }

        private void KillProcess ( string processName ) {

            Process[ ] prcs = Process.GetProcesses( );
            foreach (Process p in prcs) {
                if (p.ProcessName.Equals(processName)) {
                    p.Kill( );
                }
            }
        }
        private bool FindProcess(string processName){

            Process[ ] prcs = Process.GetProcesses( );
            foreach (Process p in prcs) {
                if (p.ProcessName.Equals(processName)) {
                    return true;
                }
            }
            return false;
        }

        private void RunExApplication ( string appName ) {

            Process mPro = new Process( );
            string path = System.AppDomain.CurrentDomain.BaseDirectory;
            mPro.StartInfo.FileName = path + "\\" + appName;
            mPro.Start( );
        }

        private void btnDownLoad_Click ( object sender, EventArgs e ) {


            //检测主程序是否在运行
            if (FindProcess("ProShoppingPen"))
                if (DialogResult.Yes == MessageBox.Show("点读笔程序正在运行，是否关闭？", "自动升级", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                {
                    KillProcess("ProShoppingPen");
                    KillProcess("vfwcap");
                }else
                    this.Close( );
            
            au.StartDownload(this.au,downloadFileList);

            MessageBox.Show("程序需要重新启动才能应用更新，请点击确定重新启动程序。", "自动更新", MessageBoxButtons.OK, MessageBoxIcon.Information);
            try
            {           
                RunExApplication("ProShoppingPen.exe");
            }
            finally
            {
               this.Close( );            	
            }




        }

        private void FormLiveUpdate_Load ( object sender, EventArgs e ) {

            string autoUpdate = null;
            try
            {
                IniFile iniFileOpt = new IniFile(AppDomain.CurrentDomain.BaseDirectory + "config.ini");

                autoUpdate = iniFileOpt.IniReadValue("config", "autoUpdate");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("配置文件丢失！");
                this.Close( );
            }

            try
            {
                au = new AutoUpdater( );

                if (au.NeedUpdate( )) {

                    downloadFileList = au.DownloadList;
                    foreach (DownloadFileInfo file in this.downloadFileList) {
                        ListViewItem item = new ListViewItem(new string[ ] { file.FileName, file.LastVer, file.Size.ToString( ) });
                        this.listDownloadFile.Items.Add(item);
                    }

                    this.Activate( );
                    this.Focus( );
                } else {
                    if (!autoUpdate.Equals("1"))
                        MessageBox.Show("已是最新版本，不需要更新！");
                    this.Close( );
                }
            }
            catch (System.Exception ex)
            {
                if (!autoUpdate.Equals("1"))
                    MessageBox.Show("服务器或网络错误，请进行检查！");
                this.Close( );
            }

        }

        private void btnCancel_Click ( object sender, EventArgs e ) {

            this.DialogResult = DialogResult.Cancel;
            this.Close( );
        }
        
    }
}
